package com.weds.edpf.core.web;

import com.alibaba.fastjson.JSONObject;
import com.weds.bean.jwt.JwtUtils;
import com.weds.core.utils.StringUtils;
import com.weds.edpf.core.entity.AcUserVO;
import com.weds.edpf.core.entity.NTtCardEntity;
import com.weds.edpf.core.entity.UserCardVO;
import com.weds.edpf.core.entity.WtPublicEntity;
import com.weds.edpf.core.service.NTtCardService;
import com.weds.edpf.core.service.NWtRegService;
import com.weds.edpf.core.service.WtModuleService;
import com.weds.edpf.core.service.WtPublicService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;

import com.weds.core.base.BaseController;
import com.weds.core.annotation.Logs;
import com.weds.core.resp.JsonResult;
import com.weds.core.base.BaseCommPager;
import com.weds.core.base.BasePageSearch;


import com.weds.edpf.core.entity.NDtCardEntity;
import com.weds.edpf.core.service.NDtCardService;

/**
 * @Author wangjianqiang
 * @Description 卡 管理
 * @Date 2020-06-16
 * @Version 新增: 发卡，已发卡列表，未发卡列表接口  wangjianqiang  2020/06/17
 * 新增: 挂失，解挂,退卡接口  wangjianqiang  2020/06/18
 */
@RestController
@RequestMapping(value = "/card")
@Api(value = "卡管理", description = "卡管理")
public class NDtCardController extends BaseController {
    @Autowired
    private NDtCardService nDtCardService;

    @Autowired
    private NTtCardService nTtCardService;

    @Autowired
    NWtRegService nWtRegService;

    @Autowired
    WtModuleService wtModuleService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    WtPublicService wtPublicService;

    private Logger log = LogManager.getLogger();

    /**
     * 发卡
     *
     * @param record
     * @return
     */
    @Logs
    @ApiOperation(value = "发卡", notes = "发卡")
    @RequestMapping(value = "/cardIssue", method = RequestMethod.POST)
    public JsonResult<Object> cardIssue(@RequestBody NDtCardEntity record) {

        if (record == null || record.getUserSerial() == null || record.getCardLx() == null || StringUtils.isBlank(record.getCardHao()) || StringUtils.isBlank(record.getUserNo())) {
            return failMsg("参数错误");
        }

        // 验证物理卡号
        NDtCardEntity nDtCardEntity = nDtCardService.selectByCardNo(record.getCardHao());
        if (nDtCardEntity != null) {
            if (nDtCardEntity.getLx() == null || nDtCardEntity.getLx() != 0) {
                return failMsg("非员工卡,请检查卡片是否为商户卡或管理卡");
            }

            if (!record.getUserNo().equals(nDtCardEntity.getUserNo())) {
                return failMsg("卡已被" + nDtCardEntity.getUserLname() + "使用");
            }

            if (nDtCardEntity.getCardType() == 1) {
                return failMsg("卡片已经挂失");
            }
        }

        int cards = nDtCardService.countCardType(record.getUserSerial(), 4);
        if (cards != 0) {
            return failMsg("人员存在未完成的发卡记录,请换成之前的卡片继续发卡");
        }
        //是否需要授权
        int access = nWtRegService.selectCardShouQ();
        if (access == 1) {
            int countXh = nWtRegService.countXh();
            if (countXh != 0) {
                return failMsg("卡片未授权,请确认卡片是否授权");
            }
        }

        record.setLx(0);
        record.setCardWork(0);
        record.setCardType(4);
        //获取cardId
        Integer cardId = wtModuleService.selectCardId();
        //8位10进制数
        String cardSerival = String.format("%0" + 8 + "d", cardId + 1);
        record.setCardSerial(cardSerival);
        String glyNo = JwtUtils.getJwtData(request).getString("userSerial");
        String ip = request.getRemoteAddr();
        record.setIp(ip);
        record.setGlyNo(glyNo);

        //发卡流程
        JsonResult<Object> result = nDtCardService.cardIssue(record);
        return result;
    }

    /**
     * 已发卡列表
     *
     * @param record
     * @return
     */
    @Logs
    @ApiOperation(value = "已发卡列表", notes = "已发卡列表")
    @RequestMapping(value = "/cardIssueList", method = RequestMethod.POST)
    public JsonResult<BaseCommPager<UserCardVO>> cardIssueList(@RequestBody BasePageSearch<UserCardVO> record) {
        setPageHelper(record);
        UserCardVO entity = record.getSearch();
        if (entity == null) {
            entity = new UserCardVO();
        }
        //列表查询
        List<UserCardVO> list = nDtCardService.cardIssueList(entity);

        return succMsgData(new BaseCommPager<>(list));
    }

    /**
     * 未发卡列表
     *
     * @param record
     * @return
     */
    @Logs
    @ApiOperation(value = "未发卡列表", notes = "未发卡列表")
    @RequestMapping(value = "/cardNotIssueList", method = RequestMethod.POST)
    public JsonResult<BaseCommPager<UserCardVO>> cardNotIssueList(@RequestBody BasePageSearch<UserCardVO> record) {
        setPageHelper(record);
        UserCardVO entity = record.getSearch();
        if (entity == null) {
            entity = new UserCardVO();
        }
        //列表查询
        List<UserCardVO> list = nDtCardService.cardNotIssueList(entity);
        return succMsgData(new BaseCommPager<>(list));
    }

    /**
     * 退卡
     *
     * @param record
     * @return
     */
    @Logs
    @ApiOperation(value = "退卡", notes = "退卡")
    @RequestMapping(value = "/cardBack", method = RequestMethod.POST)
    public JsonResult<Object> cardBack(@RequestBody NDtCardEntity record) {

        if (record == null || record.getUserSerial() == null || StringUtils.isBlank(record.getCardHao()) || StringUtils.isBlank(record.getCardSerial())) {
            return failMsg("参数错误");
        }

        //查询卡类型信息
        NTtCardEntity nTtCardEntity = nTtCardService.selectByCardServial(record.getCardSerial());
        record.setCardLx(nTtCardEntity.getTtXh());
        record.setCardType(4);
        record.setLx(0);
        String glyNo = JwtUtils.getJwtData(request).getString("userSerial");
        String ip = request.getRemoteAddr();
        record.setIp(ip);
        record.setGlyNo(glyNo);

        //退卡流程
        JsonResult<Object> result = nDtCardService.cardBacks(record);
        return result;

    }


    /**
     * 挂失
     *
     * @param record
     * @return
     */
    @Logs
    @ApiOperation(value = "挂失", notes = "挂失")
    @RequestMapping(value = "/cardloss", method = RequestMethod.POST)
    public JsonResult<Object> cardloss(@RequestBody NDtCardEntity record) {
        if (record == null || record.getUserSerial() == null || StringUtils.isBlank(record.getCardHao())) {
            return failMsg("参数错误");
        }

        String glyNo = JwtUtils.getJwtData(request).getString("userSerial");
        String ip = request.getRemoteAddr();
        record.setIp(ip);
        record.setGlyNo(glyNo);
        record.setLx(1);
        //挂失流程
        JsonResult<Object> result = nDtCardService.cardloss(record);
        return result;
    }


    /**
     * 解挂
     *
     * @param record
     * @return
     */
    @Logs
    @ApiOperation(value = "解挂", notes = "解挂")
    @RequestMapping(value = "/cardUnloss", method = RequestMethod.POST)
    public JsonResult<Object> cardUnloss(@RequestBody NDtCardEntity record) {

        if (record == null || record.getCardLx() == null || record.getUserSerial() == null || record.getCardHao() == null) {
            return failMsg("参数错误");
        }
        String glyNo = JwtUtils.getJwtData(request).getString("userSerial");
        String ip = request.getRemoteAddr();
        record.setIp(ip);
        record.setGlyNo(glyNo);
        record.setLx(1);
        //解挂流程
        JsonResult<Object> result = nDtCardService.cardUnloss(record);
        return result;
    }


    /**
     * 查询账户信息
     *
     * @param userSerival
     * @return
     */
    @Logs
    @ApiOperation(value = "查询账户信息", notes = "查询账户信息")
    @RequestMapping(value = "/selectAcInfo", method = RequestMethod.GET)
    public JsonResult<Object> selectAcInfo(@RequestParam("userSerival") Long userSerival) {

        if (userSerival == null) {
            return failMsg("参数错误");
        }
        //查询人员信息
        AcUserVO acUserInfo = nDtCardService.selectAcUserInfo(userSerival);
        //查询账户信息
        AcUserVO acInfo = nDtCardService.selectAcInfo(userSerival + "");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("acUserInfo", acUserInfo);
        jsonObject.put("acInfo", acInfo);

        return succMsg("操作成功", jsonObject);
    }

}